package com.zhan.ielts.dao.impl;

import java.util.List;
import java.util.Map;

import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.zhan.frame.core.dao.impl.BaseDaoImpl;
import com.zhan.frame.core.exception.DaoException;
import com.zhan.frame.core.utils.BeanUtils;
import com.zhan.ielts.dao.SentenceDao;
import com.zhan.ielts.domain.Sentence;
import com.zhan.ielts.domain.query.SentenceQuery;
import com.zhan.ielts.domain.vo.SentenceVo;

@Repository
public class SentenceDaoImpl extends BaseDaoImpl<Sentence> implements
		SentenceDao {

	@Override
	public List<String> selectType(Sentence query) {
		try {
			Map<String, Object> params = BeanUtils.toMap(query);
			return sqlSessionTemplate.selectList(getSqlName("selectType"),
					params);
		} catch (Exception e) {
			throw new DaoException(String.format("查询type出错！语句：%s",
					getSqlName("selectType")), e);
		}

	}

	@Override
	public List<String> selectPassage(Sentence query) {
		try {
			Map<String, Object> params = BeanUtils.toMap(query);
			return sqlSessionTemplate.selectList(getSqlName("selectPassage"),
					params);
		} catch (Exception e) {
			throw new DaoException(String.format("查询type出错！语句：%s",
					getSqlName("selectPassage")), e);
		}
	}

	@Override
	public List<Integer> selectParagraphNo(Sentence query) {
		try {
			Map<String, Object> params = BeanUtils.toMap(query);
			return sqlSessionTemplate.selectList(
					getSqlName("selectParagraphNo"), params);
		} catch (Exception e) {
			throw new DaoException(String.format("查询type出错！语句：%s",
					getSqlName("selectParagraphNo")), e);
		}
	}

	@Override
	public PageImpl<SentenceVo> selectPageListCustom(SentenceQuery query,
			Pageable pageable) {
		try {
			List<SentenceVo> contentList = sqlSessionTemplate.selectList(
					getSqlName("selectCustom"), getParams(query, pageable));
			return new PageImpl<SentenceVo>(contentList, pageable,
					this.selectCount(query));
		} catch (Exception e) {
			throw new DaoException(String.format("根据分页对象查询列表出错！语句:%s",
					getSqlName("selectCustom")), e);
		}
	}

	@Override
	public List<Sentence> selectSubtypePassage(SentenceVo entity) {
		try {
			Map<String, Object> params = BeanUtils.toMap(entity);
			return sqlSessionTemplate.selectList(
					getSqlName("selectSubtypePassage"), params);
		} catch (Exception e) {
			throw new DaoException(String.format("查询对象列表出错！语句：%s",
					getSqlName("selectSubtypePassage")), e);
		}
	}

	@Override
	public List<SentenceVo> selectBySeqNo(SentenceVo entity) {
		try {
			Map<String, Object> params = BeanUtils.toMap(entity);
			return sqlSessionTemplate.selectList(getSqlName("selectBySeqNo"),
					params);
		} catch (Exception e) {
			throw new DaoException(String.format("查询对象列表出错！语句：%s",
					getSqlName("selectBySeqNo")), e);
		}
	}

	@Override
	public List<Sentence> selectByFixedConditions(SentenceVo entity) {
		try {
			Map<String, Object> params = BeanUtils.toMap(entity);
			return sqlSessionTemplate.selectList(
					getSqlName("selectByFixedConditions"), params);
		} catch (Exception e) {
			throw new DaoException(String.format("查询对象列表出错！语句：%s",
					getSqlName("selectByFixedConditions")), e);
		}
	}

}
